package com.kuang.productConsumer;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * A B C D 线程通信 JUC Lock+Condition  打印结果：A B C D 线程无序交替打印1、0
 *
 * @author sdm
 * @version 1.0
 * @date 2023/6/24 15:23
 */
public class ProductConsumerConditionDemoABCDJUC {
    public static void main(String[] args) {
        // 获取资源类
        Goods2 goods2 = new Goods2();

        // 创建多线程，将资源类抛进去
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    goods2.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    goods2.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    goods2.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    goods2.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();

        /* 打印结果：A B C D 线程无序交替打印1、0
            A=>1
            B=>0
            A=>1
            B=>0
            A=>1
            B=>0
            A=>1
            B=>0
            A=>1
            B=>0
            A=>1
            B=>0
            A=>1
            B=>0
            A=>1
            B=>0
            A=>1
            B=>0
            A=>1
            B=>0
            C=>1
            D=>0
            C=>1
            D=>0
            C=>1
            D=>0
            C=>1
            D=>0
            C=>1
            D=>0
            C=>1
            D=>0
            C=>1
            D=>0
            C=>1
            D=>0
            C=>1
            D=>0
            C=>1
            D=>0

         */
    }

}

// 等待，业务，通知
class Goods2{
    private int number = 0;

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void increment() throws InterruptedException {
        lock.lock();
        try {
            // 判断等待
            while (number != 0){
                condition.await();
            }
            // 业务操作
            number++;
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            // 通知其他线程，我+1操作完成了
            condition.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            // 判断等待
            while (number == 0){
                condition.await();
            }
            // 业务操作
            number--;
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            // 通知其他线程，我+1操作完成了
            condition.signalAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
}
